package A11JDK7time;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws ParseException {
        /*JDK7中Data
        Date d = new Date();//当前时间
        System.out.println(d);
        Date d2 = new Date(1000L);//从初始时间过数据毫秒的时间
        System.out.println(d2);
        //d.setTime(1000);//修改时间
        //System.out.println(d);
        long dt=d.getTime();
        d2.setTime(dt+ 1000L *60*60*24*365);
        System.out.println(d2);*/

        /*JDK7中SimpleDataFormat
        //带参构造
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//y年m月d天h小时m分钟s秒
        Date date = new Date(0L);
        System.out.println(sdf.format(date));
        //空参构造
        SimpleDateFormat sdf1 = new SimpleDateFormat();
        Date date1 = new Date(0L);
        System.out.println(sdf1.format(date1));
        //format函数将时间对象变为字符串
        //parse函数将字符串转为时间对象
        String str="2023-12-12 20:00:00";
        Date data3=sdf.parse(str);
        System.out.println(data3);*/

        /*JDK7中Calendar
        Calendar cal =Calendar.getInstance();//抽象类，不能直接new
        Date date = cal.getTime();
        date.setTime(1000L);
        cal.setTime(date);//Calendar的getTime与setTime方法
        //Date date1 = cal.getTime();
        //System.out.println(date1);

        long dateTime = cal.getTimeInMillis();//获取日历对象的毫秒值
        cal.setTimeInMillis(dateTime+1000L*60*60*24);//通过毫秒值设置日历对象时间
        //Date date2 = cal.getTime();
        //System.out.println(date2);

        String str="2023-12-12 08:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal.setTime(sdf.parse(str));
        //cal.set(Calendar.YEAR, 8);//通过位置进行修改
        Date date1 = cal.getTime();
        System.out.println(date1);
        int year = cal.get(Calendar.YEAR);//获得某个字段的信息
        int month = cal.get(Calendar.MONTH);//月份取值范围为0-11
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println(year);
        System.out.println(dayOfWeek);//此处输出三，因为外国人把星期天当作一周第一天
        cal.add(Calendar.YEAR, 2020);//对某个字段进行增减，减少时用负数
        Date date2 = cal.getTime();
        System.out.println(date2);
        */
    }
}
